/**
 * Created by IntelliJ IDEA.
 * User: jiyufei
 * Date: 2018/6/5
 * Time: 15:59
 **/
package com.jiyufei.log;

import com.jiyufei.util.ElasticSearchInfo;
import com.jiyufei.util.LogUtil;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ElasticSearchLog {
    
    private JestClient jestClient;
    private Index index;
    private LogUtil logUtil;
    private SimpleDateFormat simpleDateFormat;
    
    public ElasticSearchLog(){
        JestClientFactory jestClientFactory = new JestClientFactory();
        jestClientFactory.setHttpClientConfig(new HttpClientConfig.Builder(ElasticSearchInfo.ElasticSearchIp).multiThreaded(true).build());
        jestClient = jestClientFactory.getObject();
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ssZZ");
    }
    
    public  void SendLog(LogUtil logUtil){
        try {
            String time = simpleDateFormat.format(new Date());
            String[] arr = time.split(" ");
            time = arr[0] + "T" + arr[1];
            logUtil.setTime(time);
            index = new Index.Builder(logUtil).index("hlas-log").type(logUtil.getType()).build();
            jestClient.execute(index);
        }catch (Exception e){
        
        }
    }
    
    public void SendLog(String type,String topology,String department,String module,String risk,String message){
        try {
            LogUtil logUtil = new LogUtil();
            String time = simpleDateFormat.format(new Date());
            String[] arr = time.split(" ");
            time = arr[0] + "T" + arr[1];
            logUtil.setTime(time);
            logUtil.setType(type);
            logUtil.setDepartment(department);
            logUtil.setTopology(topology);
            logUtil.setMessage(message);
            logUtil.setModule(module);
            logUtil.setRisk(risk);
            index = new Index.Builder(logUtil).index("hlas-log").type(type).build();
            jestClient.execute(index);
        }catch (Exception e){
        
        }
    }

}
